package com.kevin.thread;

import javax.swing.plaf.synth.SynthRadioButtonMenuItemUI;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 线程安全问题
 */
public class ThreadSecurityTest {
    public static void main(String[] args) {
        Thread2 thread= new Thread2();
        Thread t1 = new Thread(thread);
        Thread t2 = new Thread(thread);
        t1.start();
        t2.start();
    }
}

class Thread2 implements Runnable{

    private Integer tickets = 100;
    private ThreadLocal threadLocal =  new ThreadLocal();
    @Override
    public synchronized void run() {
        threadLocal.set("局部变量");
        while(true){
            if(tickets>0){
                System.out.println(Thread.currentThread().getName()+"拿到第"+tickets--+"张票");
                System.out.println("局部变量："+threadLocal.get());
            }
        }
    }
}
